#include "lcd.h"

static unsigned int fb_base;
static int xres, yres, bpp;


static void fb_get_lcd_params(void)
{
    get_lcd_params(&fb_base,  &xres, &yres, &bpp);
}

static unsigned short convert32bppto16bpp(unsigned int color)
{
    int r = (color >> 16) & 0xff;
    int g = (color >> 8) & 0xff;
    int b = (color & 0xff);

    r = r >> 3;
    g = g >> 2;
    b = b >> 3;

    return ((r << 11) | (g << 5) | b);


}

/*
** 实现画点的操作
** color 默认32位  0x00RRGGBB
*/
void fb_draw_pixel(int x, int y, unsigned int color)
{
    unsigned char *pc;    /* 8bpp */
    unsigned short *pw;   /* 16bpp */
    unsigned int *pdw;     /* 32bpp */

    unsigned int pixel_base = fb_base + (xres * bpp / 8 ) * y + x * bpp / 8;
    fb_get_lcd_params();   
    
    switch(bpp)
    {
        case  8: pc = (unsigned char *)pixel_base;
                 *pc = color;
                 break;
        case 16: pw = (unsigned short *)pixel_base;
                 *pw = convert32bppto16bpp(color);
                 break;
        case 32: pdw = (unsigned int *)pixel_base;
                 *pdw = color;
                 break;
    }
}
